package com.shenxuchen.crm.service;

import com.shenxuchen.crm.mapper.TransactionMapper;
import com.shenxuchen.crm.pojo.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class TransactionServiceImpl implements TransactionService {
    @Resource
    private TransactionMapper transactionMapper;

    public Page getPage(Map search, Integer currentPage, Integer rowsPerPage) {
        Page page = new Page();
        page.setCurrentPage(currentPage);
        page.setRowsPerPage(rowsPerPage);

        // 总记录数
        Integer totalRows = transactionMapper.getCount(search);

        // 总页数: (总记录数 - 1) / 每页条数 + 1
        Integer totalPages = (totalRows - 1) / page.getRowsPerPage() + 1;

        // 分页数据 limit startIndex, length
        /*
            00 10   第1页
            10 10   第2页
            20 10   第3页
            ...
            n 10    第n页
            n的公式：(当前页-1) * 每页条数
         */
        Integer startIndex = (page.getCurrentPage() - 1) * page.getRowsPerPage();
        List data = transactionMapper.getPageData(startIndex, page.getRowsPerPage(), search);

        page.setTotalRows(totalRows);
        page.setTotalPages(totalPages);
        page.setData(data);

        return page;
    }
}
